package com.springboot.cloud.demos.producer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.cloud.demos.entity.po.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select("select count(a.id) from t_user a" +
            " left join t_follow b on a.id= b.follow_user_id" +
            " WHERE b.follow_user_id=#{userId} ")
    Integer getFollowMeCount(@Param("userId") String userId);

    @Select("select count(a.id) from t_user a" +
            " left join t_follow b on a.id= b.user_id" +
            " WHERE b.user_id=#{userId} ")
    Integer getMyFollowCount(@Param("userId") String userId);

    @Select("select b.* from t_follow a" +
            "  join t_user b on a.follow_user_id= b.id and a.user_id=${userId}" +
            " ${ew.customSqlSegment}")
    IPage<User> getMyFollow(Page page,@Param("userId")String userId,@Param("ew")QueryWrapper queryWrapper);

    @Select("select b.* from t_follow a " +
            "  join t_user b on a.user_id= b.id and a.follow_user_id=${followUserId} " +
            " ${ew.customSqlSegment}")
    IPage<User> getFollowMe(Page page, @Param("followUserId")String followUserId,@Param("ew")QueryWrapper queryWrapper);


    @Select("SELECT a.id,a.nickname,a.avatar,a.province,a.city ,b.city as cityPosition,b.province as provincePosition,a.sex,a.job,a.real_man as realMan,a.pretty,a.birthday,ROUND(\n" +
            "                    6378.138 * 2 * ASIN(\n" +
            "                        SQRT(\n" +
            "                            POW(\n" +
            "                                SIN(\n" +
            "                                    (\n" +
            "                                        ${latitude} * PI() / 180 - b.latitude * PI() / 180\n" +
            "                                    ) / 2\n" +
            "                                ),\n" +
            "                                2\n" +
            "                            ) + COS(${latitude} * PI() / 180) * COS(b.latitude * PI() / 180) * POW(\n" +
            "                                SIN(\n" +
            "                                    (\n" +
            "                                        ${longitude} * PI() / 180 - b.longitude * PI() / 180\n" +
            "                                    ) / 2\n" +
            "                                ),\n" +
            "                                2\n" +
            "                            )\n" +
            "                        )\n" +
            "                    ) * 1000\n" +
            "                ) AS distance\n" +
            "               from t_user as a\n" +
            "                 left join t_user_position b on a.id=b.user_id\n" +
            "         ${ew.customSqlSegment}    order by distance asc")
    IPage<Map> nearBy(Page<User> page, @Param("latitude") double latitude,@Param("longitude") double longitude, @Param("ew")QueryWrapper<User> queryWrapper);
}
